package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.ProductCategoryService;
import com.mdd.admin.util.ProductCategoryUtil;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.ProductCategoryVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.PlatformUser;
import com.mdd.common.entity.ProductCategory;
import com.mdd.common.mapper.ProductCategoryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private ProductCategoryUtil productCategoryUtil;

    /**
     * 分页查询
     * @param params 参数
     * @param id
     * @return
     */
    @Override
    public PageResult<ProductCategory> list(PageValidate params, Integer id) {
        Integer pageNo = params.getPageNo();
        Integer pageSize = params.getPageSize();

        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", id);
        IPage<ProductCategory> iPage = productCategoryMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }


    /*
     *查询父级分类
     * @param id
     * @return
     */
    public List<ProductCategoryVo> list(Integer id) {
        // 查询所有分类
        List<ProductCategory> allCategories = productCategoryMapper.selectList(new QueryWrapper<>());

        // 将分类数据转化为树状结构，减少数据库查询
        Map<Integer, List<ProductCategory>> categoryMap = buildCategoryMap(allCategories);

        // 查询当前层级的分类
        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", id);
        List<ProductCategory> productCategories = productCategoryMapper.selectList(queryWrapper);

        // 组装结果
        List<ProductCategoryVo> productCategoryVos = new ArrayList<>();
        for (ProductCategory category : productCategories) {
            ProductCategoryVo vo = convertToVo(category);
            vo.setChildList(getChildCategories(category.getId(), categoryMap));  // 获取子分类
            productCategoryVos.add(vo);
        }

        return productCategoryVos;
    }
    private Map<Integer, List<ProductCategory>> buildCategoryMap(List<ProductCategory> allCategories) {
        Map<Integer, List<ProductCategory>> categoryMap = new HashMap<>();
        for (ProductCategory category : allCategories) {
            categoryMap.computeIfAbsent(Integer.parseInt(category.getParentId()), k -> new ArrayList<>()).add(category);
        }
        return categoryMap;
    }
    private List<ProductCategoryVo> getChildCategories(Integer parentId, Map<Integer, List<ProductCategory>> categoryMap) {
        List<ProductCategory> children = categoryMap.getOrDefault(parentId, Collections.emptyList());
        List<ProductCategoryVo> childVos = new ArrayList<>();
        for (ProductCategory child : children) {
            ProductCategoryVo childVo = convertToVo(child);
            childVo.setChildList(getChildCategories(child.getId(), categoryMap));  // 递归获取子分类
            childVos.add(childVo);
        }
        return childVos;
    }

    private ProductCategoryVo convertToVo(ProductCategory category) {
        ProductCategoryVo vo = new ProductCategoryVo();
        // 使用 Spring 的 BeanUtils 工具类复制属性
        BeanUtils.copyProperties(category, vo);
        return vo;
    }
    @Override
    public ProductCategory save(ProductCategory productCategory) {
        productCategoryMapper.insert(productCategory);
        return productCategory;
    }

    @Override
    public Boolean update(ProductCategory productCategory) {
        UpdateWrapper<ProductCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",productCategory.getId());
        int update = productCategoryMapper.update(productCategory, updateWrapper);
        return update>0;
    }

    @Override
    public Boolean remove(Integer id) {
        QueryWrapper<ProductCategory> productCategoryQueryWrapper = new QueryWrapper<>();
        productCategoryQueryWrapper.eq("parent_id",id).or().eq("id",id);
        int delete = productCategoryMapper.delete(productCategoryQueryWrapper);
        return delete>0;
    }

    @Override
    public ProductCategory info(Integer id) {
        QueryWrapper<ProductCategory> productCategoryQueryWrapper = new QueryWrapper<>();
        productCategoryQueryWrapper.eq("id",id);
        return productCategoryMapper.selectOne(productCategoryQueryWrapper);
    }
}
